const { Controller } = require("egg");

class TopicLessonVideoController extends Controller {
  async list() {
    const { lessonId } = this.ctx.query;
    if (!lessonId) {
      return this.ctx.helper.error("lessonId");
    }
    try {
      const videos = await this.service.topicLessonVideo.list(
        this.ctx.helper.toInt(lessonId)
      );
      const arr = videos.map(item => {
        return {
          ...item,
          ...item.infos || {}
        }
      });
      this.ctx.helper.success(arr);
    } catch (error) {
      this.ctx.helper.error("获取视频列表失败：" + error);
    }
  }

  async getById() {
    const { id } = this.ctx.query;
    const video = await this.service.topicLessonVideo.get(
      this.ctx.helper.toInt(id)
    );
    this.ctx.helper.success(video);
  }

  async create() {
    const { ctx } = this;
    let {
      topicId,
      lessonId,
      title,
      coverImage,
      type,
      originalVideoLink,
      streamingVideoLink,
      desc,
    } = ctx.request.body;

    if (!topicId || !lessonId || !title || !originalVideoLink) {
      return this.ctx.helper.error(
        "必填项缺失，请检查topicId、title、coverImage、streamingVideoLink"
      );
    }
    lessonId = this.ctx.helper.toInt(lessonId);
    topicId = this.ctx.helper.toInt(topicId);

    try {
      const video = await this.service.topicLessonVideo.create({
        topicId,
        lessonId,
        title,
        coverImage,
        type,
        originalVideoLink,
        streamingVideoLink,
        desc,
      });
      this.ctx.helper.success(video, "视频创建成功");
    } catch (error) {
      this.ctx.helper.error("创建视频失败：" + error.message);
    }
  }

  async delete() {
    const { id } = this.ctx.request.body;
    if (!id) {
      return this.ctx.helper.error("参数异常：缺少视频ID");
    }
    try {
      const deleted = await this.service.topicLessonVideo.delete(id);
      if (!deleted) {
        return this.ctx.helper.error("视频不存在或删除失败");
      }
      this.ctx.helper.success(deleted, "视频删除成功");
    } catch (error) {
      this.ctx.helper.error("删除视频失败：" + error.message);
    }
  }

  async update() {
    const { id, ...updateData } = this.ctx.request.body;
    if (!id) {
      return this.ctx.helper.error("参数异常：缺少视频ID");
    }
    try {
      const updatedVideo = await this.service.topicLessonVideo.update(
        id,
        updateData
      );
      if (!updatedVideo) {
        return this.ctx.helper.error("视频不存在或更新失败");
      }
      this.ctx.helper.success(updatedVideo, "视频信息更新成功");
    } catch (error) {
      this.ctx.helper.error("更新视频信息失败：" + error.message);
    }
  }

  async updateStreaming() {
    const { id } = this.ctx.request.body;
    const data = await this.service.topicLessonVideo.get(
      this.ctx.helper.toInt(id)
    );
    console.log(data, id);
    const { streamingVideoLink, transcodingStatus, originalVideoLink } = data;
    if (streamingVideoLink) {
      return this.ctx.helper.error("此视频已转码，无需再转码");
    }

    if (!originalVideoLink) {
      return this.ctx.helper.error("无上传视频，请上传视频");
    }

    if (transcodingStatus !== 0) {
      return this.ctx.helper.error("此视频正在转码中，请稍后再试");
    }

    this.service.topicLessonVideo.updateStreaming(id);

    this.ctx.helper.success(data);
  }
}

module.exports = TopicLessonVideoController;
